package bb.lanxing.adapter;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class AbsHeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements IRVHeaderAdapter {
    protected static final int POSITION_MASK = 0xFFFF;
    protected static final int TYPE_FOOTER = 0x20000;
    protected static final int TYPE_HEADER = 0x10000;
    protected static final int TYPE_MASK = -0x10000;
    private List<View> mFooterView;
    private List<View> mHeaderView;

    public View getHeaderView(int index) {
        List<View> list = this.mHeaderView;
        if (list == null || index >= list.size()) {
            return null;
        }
        return this.mHeaderView.get(index);
    }

    public View getFooterView(int index) {
        List<View> list = this.mFooterView;
        if (list == null || index >= list.size()) {
            return null;
        }
        return this.mFooterView.get(index);
    }

    @Override
    public void addHeaderView(View view) {
        if (this.mHeaderView == null) {
            this.mHeaderView = new ArrayList<>();
        }
        this.mHeaderView.add(view);
    }

    @Override
    public void addFooterView(View view) {
        if (this.mFooterView == null) {
            this.mFooterView = new ArrayList<>();
        }
        int itemCount = getItemCount();
        this.mFooterView.add(view);
        notifyItemInserted(itemCount);
    }

    @Override
    public void removeHeaderView(View view) {
        List<View> list = this.mHeaderView;
        if (list == null || view == null) {
            return;
        }
        removeHeaderView(list.indexOf(view));
    }

    @Override
    public void removeFooterView(View view) {
        List<View> list = this.mFooterView;
        if (list == null || view == null) {
            return;
        }
        removeFooterView(list.indexOf(view));
    }

    @Override
    public void removeHeaderView(int index) {
        List<View> list = this.mHeaderView;
        if (list == null || index < 0 || index >= list.size()) {
            return;
        }
        if (this.mHeaderView.remove(this.mHeaderView.get(index))) {
            notifyItemRemoved(getHeaderCount() - index);
        }
    }

    @Override
    public void removeFooterView(int index) {
        List<View> list = this.mFooterView;
        if (list == null || index < 0 || index >= list.size()) {
            return;
        }
        View view = this.mFooterView.get(index);
        int itemCount = (getItemCount() - this.mFooterView.size()) + index;
        if (!this.mFooterView.remove(view)) {
            return;
        }
        notifyItemRemoved(itemCount + 1);
    }

    @Override
    public int getHeaderCount() {
        List<View> list = this.mHeaderView;
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public int getFooterCount() {
        List<View> list = this.mFooterView;
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public boolean isHeaderView(int position) {
        return position < getHeaderCount();
    }

    public boolean isFooterView(int position) {
        return position >= getItemCount() - getFooterCount();
    }

    public int generateHeaderType(int position) {
        return TYPE_HEADER | ((getHeaderCount() - position) - 1);
    }

    public int generateFooterType(int position) {
        return (position - (getItemCount() - getFooterCount())) | TYPE_FOOTER;
    }
}
